<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['missionName']" placeholder="请输入任务名称" :disabled ="true"></a-input>
        </a-form-item>

        <a-form-item label="任务奖励" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['missionRewards']" placeholder="请输入任务奖励" style="width: 40%"/>
        </a-form-item>

        <a-form-item label="执行次数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--<a-textarea v-decorator="['executionTimes']" rows="4" placeholder="请输入信息内容"/>-->
          <a-select v-model="frequency" placeholder="请选择执行类型" style="width: 40%" @change="">
            <a-select-option value="1">执行一次</a-select-option>
            <a-select-option value="2">每日执行</a-select-option>
          </a-select>
          <a-input-number v-decorator="['executionTimes']" placeholder="请输入每日执行次数" style="width: 40%" v-if="frequency == 2" :min="1"/>
        </a-form-item>

        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--<a-textarea v-decorator="['startStatus']" rows="4" placeholder="请输入信息内容"/>-->
          <a-switch default-checked @change="onChange" v-model="startStatus">
            <a-icon slot="checkedChildren" type="check" />
            <a-icon slot="unCheckedChildren" type="close" />
          </a-switch>
        </a-form-item>

        <a-form-item label="任务内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['missionContent']" rows="4" placeholder="请输入信息内容"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: "SmsMissionSerupModal",
    components: {
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/mission/smsMissionSerup/add",
          edit: "/mission/smsMissionSerup/edit",
        },
        frequency: "1",
        startStatus: true,
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.frequency = this.model.executionTimes == 0 ?"1":"2";
        this.startStatus = this.model.startStatus == 0 ? true : false;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'missionName','missionRewards','missionContent','createBy','createTime','updateBy','updateTime','delFlag','executionTimes','startStatus'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'missionName','missionRewards','missionContent','createBy','createTime','updateBy','updateTime','delFlag'))
      },
      choose(value){
        if (value == "1") {
          this.model.executionTimes = 0;
        }
      },
      onChange(checked) {
        console.log(checked);
        if (checked){
          this.model.startStatus = 0;
        } else {
          this.model.startStatus = 1;
        }
        console.log(this.model.startStatus);
      },

    }
  }
</script>